function inform() {
	if ("none" == $("#message").css("display")) {
		$("#all").attr("class", "ng-scope aside-open aside-with-am-slide-right aside-open-inner modal-open");
		$("#message").css("display", "block");
	} else {
		$("#all").removeClass();
		$("#all").addClass("ng-scope");
		$("#message").css("display", "none");
	}
}

function show() {
	if ("none" == $("#mess").css("display")) {
		$("#mess").css("display", "block");

	} else {
		$("#mess").css("display", "none");
	}
}
$('#user-action').off('click').on('click', function() {
	show();
});

$('#content').off('click').on('click', function() {
	$("#mess").css("display", "none");
});

// 用于封装js
var jsApp = {};
// html模板函数
var simpleTpl = function( tpl ) {
    tpl = $.isArray( tpl ) ? tpl.join( '' ) : (tpl || '');
    return {
        store: tpl,
        _: function() {
            var me = this;
            var tempStr="";
            var res=arguments[1]||"";
            if(arguments.length==2&&typeof res=="object"&&typeof res.length=="undefined"){
            	var tempStr="";
            	var argStr=arguments[0];
            	for(var i in res){
            		tempStr+=' '+i+'=\"'+res[i]+'\"';
            	}
            	if(/^<img/.test(argStr)&&argStr.lastIndexOf("/")!=-1){
            		tempStr=argStr.substring(0,argStr.lastIndexOf("/"))+tempStr+'/>';
            	}else{
	            	tempStr=argStr.substring(0,argStr.length-1)+tempStr+'>';
            	}
            	me.store += tempStr;
            }else{
            	$.each(arguments,function( index, value) {
            		me.store += value;
            	});
            }
            return this;
        },
        toString: function() {
            return this.store;
        }
    };
};

var showMsg = function(type,msg){
	var n = noty({text: msg,type:type,closewith:'backdrop',theme:'bootstrapTheme'})
	setTimeout(function(){n.close()},2000)
};
var localPath;
jQuery(function(){

	//1代运营  2自运营
	var zTreeNodes ;
    if(operationType==2){
        zTreeNodes=[
            {"id":1003,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-home text-lt","name":"首页","targets":"dashboard","children":[],"url":"/dashboard?"+ new Date().getTime()},
            {"id":1004,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-list text-lt","name":"广告和计划","children":[
                {"id":10041,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告计划","targets":"campaign","children":[],"url":"/campaigns/list?"+new Date().getTime()},
                {"id":10042,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告","targets":"ads","children":[],"url":"/ads/list?"+new Date().getTime()}
            ],"url":""},
            {"id":1005,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-tint text-lt","name":"素材库","content":"","children":[
                {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告创意","targets":"creative","children":[],"url":"/front_creatives/index?"+new Date().getTime()},
                {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"落地页管理","targets":"page","children":[],"url":"/landing_pages/list?"+new Date().getTime()},
            ],"url":""},
            {"id":1006,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-stats text-lt","name":"统计报表","children":[
                {"id":10061,"parentId":1006,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告投放统计","targets":"report","children":[],"url":"/reports?"+new Date().getTime()}
                ,{"id":10062,"parentId":1006,"level":2,"icon":"fa fa-fw fa-inbox","name":"表单统计","targets":"report-form","children":[],"url":"/reports/forms?"+new Date().getTime()}
            ],"url":""},
            {"id":1007,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-user text-lt","name":"账号管理","children":[
                {"id":10071,"parentId":1007,"level":2,"name":"账号中心","targets":"profile","children":[],"url":"/profile?"+ new Date().getTime()},
                {"id":10072,"parentId":1007,"level":2,"name":"账号流水","targets":"flow","children":[],"url":"/account/flow?"+ new Date().getTime()}
            ],"url":""},

            {"id":1008,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-bell text-lt","name":"消息提醒","content":"","children":[
                {"id":10081,"parentId":1008,"level":2,"icon":"fa fa-fw fa-inbox","name":"提醒设置","targets":"setting","children":[],"url":"/ci/list?"+ new Date().getTime()},
                {"id":10082,"parentId":1008,"level":2,"icon":"fa fa-fw fa-inbox","name":"审核通知","targets":"audit","children":[],"url":"/ci/audit_list?"+ new Date().getTime()}
            ],"url":""}]

    }else if(isFromBackend==true&&system==false){
		zTreeNodes=[
		            {"id":1004,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-list text-lt","name":"广告和计划","children":[
		   												{"id":10041,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告计划","targets":"campaign","children":[],"url":"/campaigns/list?"+new Date().getTime()},
		                                               	{"id":10042,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告","targets":"ads","children":[],"url":"/ads/list?"+new Date().getTime()}
		                                                 ],"url":""},
		            {"id":1005,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-tint text-lt","name":"素材库","content":"","children":[
		                                                  {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告创意","targets":"creative","children":[],"url":"/materials/list?"+new Date().getTime()},
		                                                  {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"营销表单","targets":"form","children":[],"url":"/forms?"+new Date().getTime()}
		                                                  ],"url":""},
		            {"id":1008,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-bell text-lt","targets":"setting","name":"消息提醒","children":[],"url":"/ci/list?"+ new Date().getTime()}
		            ]
	}else if(isFromBackend==true&&system==true||isFromBackend==false){
	
		zTreeNodes=[
         {"id":1003,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-home text-lt","name":"首页","targets":"dashboard","children":[],"url":"/dashboard?"+ new Date().getTime()},
         {"id":1004,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-list text-lt","name":"广告和计划","children":[
												{"id":10041,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告计划","targets":"campaign","children":[],"url":"/campaigns/list?"+new Date().getTime()},
                                            	{"id":10042,"parentId":1004,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告","targets":"ads","children":[],"url":"/ads/list?"+new Date().getTime()}
                                              ],"url":""},
         {"id":1005,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-tint text-lt","name":"素材库","content":"","children":[
                                               {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告创意","targets":"creative","children":[],"url":"/materials/list?"+new Date().getTime()},
                                               {"id":10051,"parentId":1005,"level":2,"icon":"fa fa-fw fa-inbox","name":"营销表单","targets":"form","children":[],"url":"/forms?"+new Date().getTime()}
                                              ],"url":""},
         {"id":1006,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-stats text-lt","name":"统计报表","children":[
                                               {"id":10061,"parentId":1006,"level":2,"icon":"fa fa-fw fa-inbox","name":"广告投放统计","targets":"report","children":[],"url":"/reports?"+new Date().getTime()}
                                               ,{"id":10062,"parentId":1006,"level":2,"icon":"fa fa-fw fa-inbox","name":"表单统计","targets":"report-form","children":[],"url":"/reports/forms?"+new Date().getTime()}
                                              ],"url":""},
         {"id":1007,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-user text-lt","name":"账号管理","children":[
                                               {"id":10071,"parentId":1007,"level":2,"name":"账号中心","targets":"profile","children":[],"url":"/profile?"+ new Date().getTime()},
                                               {"id":10072,"parentId":1007,"level":2,"name":"账号流水","targets":"flow","children":[],"url":"/account/flow?"+ new Date().getTime()}
          									   ],"url":""},
         {"id":1008,"parentId":0,"level":1,"icon":"icon glyphicon glyphicon-bell text-lt","name":"消息提醒","targets":"setting","children":[],"url":"/ci/list?"+ new Date().getTime()}
];
	}
	var tbody = "";  
	var count=1;
	$.each(zTreeNodes,function(n,value) {   
		 var trs = "";  
		 var aClass=(n==0?"nav-header":"nav-header collapsed")
		 //
		 var iconArr = value.icon.split(",")
		 var prefix_icon = ""
		 var subfix_icon = ""
		 if(iconArr != null){
			 if(iconArr.length == 2){
				 prefix_icon = iconArr[0]
				 subfix_icon = iconArr[1]
			 }else{
				 prefix_icon = value.icon
			 }
		 }
		 if(value.url==null || value.url=='' || value.url=='null' || (!value.url)){
			var coun= count++;
			 trs += "<li name='l2' id='url_a"+coun+"'><a href='javascript:;' data-target='."+value.id+"-menu' class='"+aClass+"' location='"+value.targets+"' data-toggle='collapse'><i class='"+value.icon+"'></i> "+value.name+"<i class='fa fa-collapse'></i>" +
			 		"<span class='pull-right text-muted'><i class='fa fa-caret-down'></i></span></a>";  
		 }else{
			 var coun=count++;
			 if (coun == 1 || coun == 6) {  // 运营
			 	trs += "<li name='l2' id='url_a"+coun+"'  class='noSub'><a href='javascript:;' data-href='" +value.url+"' id='subli"+coun+"'  data-target='."+value.id+"-menu' class='"+aClass+"' data-toggle='collapse'><i class='"+prefix_icon+"'></i> "+value.name+"<i class='"+ subfix_icon +"'></i></a>";
			 } else { // 客户
				trs += "<li name='l2' id='url_a"+coun+"' class='noSub'><a href='javascript:;' data-href='" +value.url+"' data-target='."+value.id+"-menu' location='"+value.targets+"' class='"+aClass+"' data-toggle='collapse'><i class='"+prefix_icon+"'></i> "+value.name+"<i class='"+ subfix_icon +"'></i></a>";
			}
		 }
		 
	     $.each(value.children,function(t,childValue) { 
	    	 if(t==0){
	    		 if(n==0){
	    			 trs+=" <ul class='nav nav-sub bg '>";
	    		 }else{//nav nav-sub bg
	    			 trs+="<ul class='nav nav-sub bg '>";
	    		 }
	    	 }
	    	 if(childValue.url == null || childValue.url == ''){
	    		 trs+="<li><a href='javascript:;' data-href='" +childValue.url+"' id='subli"+(t+1)+"' >"+ childValue.name+"</a></li>";
	    	 }else{
	    		 trs+="<li><a href='javascript:;' data-href='" +childValue.url+"' id='subli"+(t+1)+"' location='"+childValue.targets+"'>"+ childValue.name+"</a></li>";
	    	 }
	    	 if(t+1==value.children.length){
	    		 trs+="</ul></li>";
	    	 }   
	     });

	     if(n+1==zTreeNodes.length){
	    	 trs+="</li>"
	     }
	     tbody+=trs;
	});
	$("#ter").append(tbody);  

	var subIndex = $.cookie("subId");
	var parentIndex = $.cookie("parentId");
	if (subIndex && parentIndex) {
		$('#'+ parentIndex).addClass('active');
		$('#'+ parentIndex).find('.nav-sub').find('#'+subIndex).addClass('active');
	}
	// 如果是二级菜单 则点击子菜单跳转
    $('#aside').find('.nav-sub li').on('click', function() {
     	var subId = $(this).find('a').attr('id');
     	var href = $(this).find('a').attr('data-href');
     	var parentId = $(this).closest('.nav-sub').closest('li').attr('id');
     	$.cookie("subId",subId, { path: '/'});
     	$.cookie("parentId",parentId, { path: '/'});
     	window.location.href = href;
    });

    // 不是二级菜单直接点击跳转  并且把值存cookie
    $('#aside').find('.noSub').on('click', function() {
     	var subId = $(this).find('a').attr('id');
     	var href = $(this).find('a').attr('data-href');
     	var parentId = $(this).attr('id');
     	$.cookie("subId",subId, { path: '/'});
     	$.cookie("parentId",parentId, { path: '/'});
     	window.location.href = href;
    });

	$("li[name='l2']").click(function(){
		$("li[name='l2']").each(function(){
			$(this).removeClass();
		});
		$(this).addClass("active");
		$.cookie("urlid",$(this).attr("id"), { path: '/'});
	});
	// var a=$.cookie("urlid")
	// if(a){
	// 	$("#"+$.cookie("urlid")).addClass("active");
	// }

	//
	if(!jQuery.isEmptyObject(errors)){
		$("form").validate().showErrors(errors)
	}
	//
	if("" !== successMsg){
		showMsg('success',successMsg)
	}
	// $("#ter").find('a').filter(function () {
	// 	return $(this).attr("location") != localPath;
	// }).removeClass('active').closest("li[name='l2']").removeClass('active');

	// $("#ter").find('a').filter(function () {
	// 	return $(this).attr("location") == localPath;
	// }).addClass('active').closest("li[name='l2']").addClass('active');
});


// 退出系统
$('.logout-btn').on('click', function() {
	var result = confirm("确认退出?");
	if (result == true) {
		$.ajax({
			url: "/logout",
			type: "POST",
			data: "",
			dataType: "json",
			cache: false,
			success: function(data) {
				$.cookie("urlid",'url_a1', { path: '/'});
				$.cookie("subId",'subli1', { path: '/'});
				$.cookie("parentId",'url_a1', { path: '/'});
				window.location.reload();
			},
			error: function() {
				$.cookie("urlid",'url_a1', { path: '/'});
				$.cookie("subId",'subli1', { path: '/'});
				$.cookie("parentId",'url_a1', { path: '/'});
				window.location.reload();
			}
		})
	} else {
		return false;
	}
});


